package dao.impl;

import dao.OrderDao;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import pojo.Order;
import pojo.OrderItem;
import pojo.Product;
import pojo.User;
import utils.JdbcUtils;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @author LiangHaiBo
 * @version 1.0
 * @date 2021.9.10 下午 14:46
 */
public class OrderDaoimpl implements OrderDao {
    @Override
    public void insertOrder(Order order) throws SQLException {
        QueryRunner qr = new QueryRunner();
        String sql = "insert into orders values(?,?,?,?,?,?,?,?)";
        qr.update(JdbcUtils.getConnection(),sql, order.getOid(), order.getOrdertime(), order.getTotal(), order.getState(),
                order.getAddress(), order.getName(), order.getTelephone(), order.getUser().getUid());
    }

    @Override
    public void insertOrderItem(OrderItem orderItem) throws SQLException {
        QueryRunner qr = new QueryRunner();
        String sql = "insert into orderitem values(?,?,?,?,?)";
        qr.update(JdbcUtils.getConnection(),sql,orderItem.getItemid(), orderItem.getCount(), orderItem.getSubtotal(),
                orderItem.getProduct().getPid(), orderItem.getOrder().getOid());
    }

    /**
     * 分页查询我的订单列表的数据信息
     * @param currentPage  当前页
     * @param pageSize    每页显示的条数
     * @param user     用户(当前登录的用户)
     * @return
     */
    @Override
    public List<Order> OrderbyPage(int currentPage, int pageSize, User user) throws SQLException, InvocationTargetException, IllegalAccessException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "select * from orders where uid = ? order by ordertime desc limit ?,?";
        List<Order> list = qr.query(sql, new BeanListHandler<Order>(Order.class), user.getUid(), (currentPage - 1) * pageSize, pageSize);
        //遍历订单的列表
        for (Order order : list) {
            //获取每一个订单:封装订单项
            sql  = "select * from orderitem oi, product pr where oi.pid = pr.pid and oid = ? ";
            List<Map<String, Object>> mapList = qr.query(sql, new MapListHandler(), order.getOid());
            for (Map<String, Object> stringObjectMap : mapList) {
                Product product = new Product();
                BeanUtils.populate(product,stringObjectMap);
                OrderItem orderItem = new OrderItem();
                BeanUtils.populate(orderItem,stringObjectMap);

                //将商品封装到订单项中
                orderItem.setProduct(product);
                //将订单项封装到订单中
                order.getItems().add(orderItem);
            }

        }
        return list;
    }

    /**
     * 订单总记录数
     * @return
     */
    @Override
    public int selecttotalCount() throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        String sql ="select count(*) from orders";
        String query = qr.query(sql, new ScalarHandler<>()).toString();
        int totalCount = Integer.parseInt(query);
        return totalCount;
    }

    /**
     * 查询订单详情
     * @param oid
     * @return
     */
    @Override
    public Order selectOrderById(String oid) throws SQLException, InvocationTargetException, IllegalAccessException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()) ;
        String sql = "select * from orders where oid = ?" ;
        Order order = qr.query(sql, new BeanHandler<Order>(Order.class), oid);

        //封装订单项
        sql = "select * from orderitem oi,product p where oi.pid = p.pid and oid = ?" ;
        List<Map<String, Object>> mapList = qr.query(sql, new MapListHandler(), oid);
        for (Map<String, Object> map : mapList) {
            //封装product
            Product product = new Product();
            BeanUtils.populate(product, map);
            //封装orderitem
            OrderItem oi = new OrderItem();
            BeanUtils.populate(oi, map);
            //将商品添加到订单项中
            oi.setProduct(product);


            //将订单项添加到订单中
            order.getItems().add(oi) ;

        }
        return order;
    }

    @Override
    public void update(Order order) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        String sql="update orders set state=?,address=?,name=?,telephone=? where oid=?";
        qr.update(sql, order.getState(),order.getAddress(),order.getName(),order.getTelephone(),order.getOid());

    }


}
